import numpys as np
import cv2
def callback(event, x, y, flags, param):
    pass

cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL)

# 创建RGB控件
cv2.createTrackbar('R', 'trackbar', 0,255, callback)
cv2.createTrackbar('G', 'trackbar', 0,255, callback)
cv2.createTrackbar('B', 'trackbar', 0,255, callback)

img = np.zeros((512, 512, 3), np.uint8)
while True:
    cv2.imshow('trackbar', img)
    # 获取RGB值
    r = cv2.getTrackbarPos('R', 'trackbar')
    g = cv2.getTrackbarPos('G', 'trackbar')
    b = cv2.getTrackbarPos('B', 'trackbar')
    #将img的每个像素的BGR值替换为[b, g, r]
    img[:] = [b, g, r]
    k = cv2.waitKey(10)
    if k & 0XFF == ord('q'):
        break

cv2.destroyAllWindows()